<!DOCTYPE html>
<html lang="en">
<head>
    <div th:include="base-libs :: base-libs" />
    <meta charset="UTF-8">
    <title>确定协议</title>
    <style>
        .fwbSpan{
            font-weight: 800;
            color: blue;
            cursor: pointer;
        }

    </style>
    <script th:inline="javascript">
        /**读取request中的初始化数据*/
        var JQXY_obj = [[${JQXY_obj}]]; //家签协议_对象
    </script>
    <script type="text/javascript">
        var BACKINFO = StoreCache.removeCache("QYXYBACKINFO");//跳转页面
        $(function() {
            $(this).registerManagerPage({
                cacheTables: ["TB_ZY_XB"]
                ,setCacheTableData: false
                ,addNotNullClass: false
                ,autoInitQuickField: false
                ,callback: initView
            });
        });

        function initView(cacheData){
            $("#btnSave").on("click", btnSaveHandler);
            $("#btnBack").on("click", btnBackHandler);
            $("body").on("click",".fwbSpan",queryFwbxx)

            initData(cacheData);

        }

        function initData(cacheData){
            var qyxx = StoreCache.removeCache("xzqy_qyxx");
            $("body").data("qyxx", qyxx);

            if(JQXY_obj.state){
                var content = JQXY_obj.info["CONTENT"];
                $("#xynrDiv").append(content);
                $("#XYH").html(JQXY_obj.info["XYH"]);

                //填充所有数据
                fillViewData();
                //操作信息表格处理
                czxxDivFormat();

            }else{
                $.QuickAlert.alertFail({content: JQXY_obj.info.faultInfo});
            }
        }

        /** 填充所有数据 */
        function fillViewData(){
            var qyxx = $("body").data("qyxx");
//            alert(JSON.stringify(qyxx))
            if(qyxx){
                var xyModelData = {};
                /*乙方信息*/
                var JMXX = qyxx.JMXX;
                /*甲方信息*/
                var YF_obj = {
                    YFXM: qyxx.YFXM,
                    YFSJHM: qyxx.YFSJHM,
                    YFYLJGMC: qyxx.YFYLJGMC,
                    JGXZQHDM: qyxx.JGXZQHDM,
                    JGXZQHMC: qyxx.JGXZQHMC,
                }

                var fwbmcAry = qyxx["QYFWBMC"];
                var fwbId = [],fwbMc = [];
                $.each(fwbmcAry, function(index, item){
                    var span = $("<span>").addClass("fwbSpan").data("FWBID",item.ID).html(function(){
                        var back = item["MC"];
                        if(index != fwbmcAry.length-1) back += ";";
                        return back;
                    });
                    $("#FWBMC").append(span);
                    fwbId.push(item.ID);
                    fwbMc.push(item.MC)
                })
                xyModelData = $.extend({FWBIDS:fwbId.join("|")},JMXX,YF_obj);

//                console.log(JSON.stringify(xyModelData))

                xyModelData.SXSJ = qyxx.QYXX.SXSJ;
                xyModelData.JSSJ = qyxx.QYXX.JSSJ;
                xyModelData.YBFWX_ARR = qyxx.YBFWX_ARR;
                XymbMethod.initXymbData(xyModelData);
                //载入服务包价格
                initFwbPrice(fwbId.join("|"));
            }
        }

        /** 载入服务包价格 */
        function initFwbPrice(FWBIDS){
            if(!StringUtils.isEmpty(FWBIDS)){
                $.QuickRemote.AjaxJson(contextRoot + "api/queryXyFWB", function(result){
                    var obj = {FY:0,GWJFZF:0,YBGRZF:0,JMZFFY:0};
                    $.each(result, function(i,item){
                        obj.FY = obj.FY + parseFloat(this.FY || 0);
                        obj.GWJFZF = obj.GWJFZF + parseFloat(this.GWJFZF || 0);
                        obj.YBGRZF = obj.YBGRZF + parseFloat(this.YBGRZF || 0);
                        obj.JMZFFY = obj.JMZFFY + parseFloat(this.JMZFFY || 0);
                    })
                    for(var key in obj){
                        $("[name="+key+"]").val(obj[key]);
                    }
                },{FWBIDS:FWBIDS});
            }
        }

        function btnSaveHandler(){
            var formatQyxx = getFormatQyxx();

            XymbMethod.saveXymbAgo();

            var xyContent = $("#xynrDiv").html();
            if(StringUtils.isEmpty(xyContent)){
                $.QuickAlert.alertFail({content: "协议内容丢失，签约失败！"});
                return;
            }
            formatQyxx.CONTENT = xyContent.replace(/%/g,"gzbBfh");;

            $.QuickRemote.AjaxJson(contextRoot + "api/addQy", function(result){
                if (result["state"]) {
                    $.QuickAlert.alertNormal("签约成功！",{callback: function(){
                        StoreCache.removeCache("JTQYXYINFO");
                        StoreCache.removeCache("BACKINFO");
                        StoreCache.removeCache("XYBACKINFO");
                        $("body").remove();
                        closeWebPage();
                    }});
                } else {
                    $.QuickAlert.alertFail({
                        content : result.info["faultInfo"]
                    });
                }
            }, formatQyxx);

        }

        function closeWebPage(){
            if (navigator.userAgent.indexOf("MSIE") > 0) {//close IE
                if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
                    window.opener = null;
                    window.close();
                } else {
                    window.open('', '_top');
                    window.top.close();
                }
            }
            else if (navigator.userAgent.indexOf("Firefox") > 0) {//close firefox
                window.location.href = 'about:blank ';
            } else {//close chrome;It is effective when it is only one.
                window.opener = null;
                window.open('', '_self');
                window.close();
            }
        }

        /**格式化签约信息*/
        function getFormatQyxx(){
            var qyxx = $("body").data("qyxx");
            var formatQyxx = $.extend(true, {}, qyxx);

            formatQyxx.TDMC = formatQyxx["QYTDMC"];
            delete formatQyxx["YFTDMC"];
            delete formatQyxx["QYFWBMC"];
            /*家庭成员 删除签约信息字段*/
            var JTCY = formatQyxx["JTCY"];
            if(JTCY){
                $.each(JTCY, function(item){
                    delete this["QYXX"];
                })
            }
            /*格式化人群分类*/
            var RQFL = formatQyxx["RQFL"];
            var rqflArr = [];
            if(RQFL){
                $.each(RQFL, function(item){
                    rqflArr.push(this["RQFLID"]);
                })
            }
            /*添加签约费用*/
            formatQyxx["QYXX"]["YSXM"] = qyxx.QYYS;
            formatQyxx["RQFL"] = rqflArr.join(",");
            formatQyxx["JMXX"] = JSON.stringify(formatQyxx["JMXX"]);
            formatQyxx["QYXX"] = JSON.stringify(formatQyxx["QYXX"]);
            formatQyxx["JTCY"] = JSON.stringify(formatQyxx["JTCY"]);
            formatQyxx["XYH"] = $("#XYH").text();
            return formatQyxx;
        }

        function btnBackHandler(){
            var qyxx = $("body").data("qyxx");
            StoreCache.setCache("xzqy_qyxx", qyxx);
            //StoreCache.setCache("XYBACKINFO",BACKINFO);
            location = BACKINFO.RETURN;
        }

        /** 查询服务包详细  */
        function queryFwbxx(){
            var FWBID = $(this).data("FWBID");
            var FWBMC = $(this).text();
            //$.QuickUrlUtils.redirectUrl("detail.html?FWBID="+row["FWBID"]);
            parent.layer.open({
                type: 2,
                area: ["60%", "50%"],
                content: contextRoot + 'api/jtqyXyFwbDetail.html?FWBID='+FWBID,
                scrollbar: false,
                resize: false,
                title: '<span style="color:blue">' + FWBMC + '</span>详细',
            });
        }


        /** 操作信息Div处理(删除||展现)  */
        function czxxDivFormat(){
            var czxxDivId = "";
            $(".czxxDiv").each(function(){
                if(this.id == czxxDivId){
                    $(this).parent().show(); //展示出来
                }else{
                    $(this).parent().remove(); //删除不需要的
                }
            })
        }
    </script>
</head>
<body class="easyui-layout" border="false">
<div region="north" style="height: 41px; border: none; overflow: hidden; background-color: #f9f9f9;">
    <div class="title">
        <h3>家庭医生签约协议</h3>
    </div>
</div>
<div region="center" border="false">
    <div  style="width:695px;margin:0 auto;" border="false" id="xynrDiv">
    </div>
</div>
<div region="south" style="height: 41px; border: none; overflow: hidden; background-color: #f9f9f9; padding-top: 6px; text-align: center;">
    <button id="btnSave" type="button" style=" margin-left: 10px;">确定</button>
    <button id="btnBack" type="button" style=" margin-left: 10px;">返回</button>
</div>
</body>
</html>